#include "..\CookHeader.h"

// ü     κ
typedef struct _Node {
	string data; // URL
	struct _Node* link = NULL;
} Node;

Array <Node*> memory;
Node* head, * current, * pre;
string currentURL;

// Լ  κ
void printNodes(Node* start) {
	if (start == NULL)
		return;
	Node* current = start;
	print(current->data);
	while (current->link != NULL) {
		current = current->link;
		print(current->data);
	}
	println("");
}

void freeMemory() { // Ҵ   ޸𸮸 Ѵ.
	for (int i = 0; i < len(memory); i++)
		delete memory[i];
}

string findPrevURL(string cUrl) {
	current = head;
	if (head->data == cUrl)
		return "";	//  ּ ȯ
	while (current->link != NULL) {
		pre = current;
		current = current->link;
		if (current->data == cUrl) {
			return pre->data;
		}
	}
}

string findNextURL(string cUrl) {
	current = head;
	if (head->data == cUrl)
		return current->link->data;
	while (current->link != NULL) {
		current = current->link;
		if (current->data == cUrl) {
			if (current->link == NULL) {
				return "";
			}
			else {
				return current->link->data;
			}
		}
	}
}

//  ڵ κ
int main() {
	string url;
	Node* node = new Node;
	while (true) {
		input(url, "URL : ");		
		if (url == "x" || url == "X")
			break;
		currentURL = url; // Է URL  ȭ
		if (head == NULL) {
			node->data = url; 
			head = node;		
		} else {
			string data = url;
			pre = node;
			node = new Node;
			node->data = data;
			pre->link = node;
		}
		memory.push_back(node);
	}
	println("--- 丮  Ϸ ---");
	printNodes(head);

	println("\n URL--> " + currentURL);

	string arrow;
	while (true) {
		input(arrow, "Ű < Ǵ > : ");
		if (arrow == "x" || arrow == "X")
			break;
		if (arrow == "<") {
			string retURL = findPrevURL(currentURL);
			if (retURL == "") {
				println("ù ° URL Դϴ.");
			}
			else {
				println(retURL + " ̵߽ϴ.");
				currentURL = retURL;
			}
		}
		else if (arrow == ">") {
			string retURL = findNextURL(currentURL);
			if (retURL == "") {
				println(" URL Դϴ.");
			}
			else {
				println(retURL + " ̵߽ϴ.");
				currentURL = retURL;
			}
		}
	}

	freeMemory();
}